<?php
$space = Blyn::app()->getActiveSpace();

$registeredMenu = $operation->getMenu();
$registeredList = array();
$registeredList['empty'] = '请选择功能';
if ($registeredMenu instanceof BAttribute) {
    $children = $registeredMenu->getChildrenAsLeaf();
    foreach ($children as $menu) {
        $registeredList[$menu->getName()] = $menu->getValue();
    }
}
//$showMenuControl = new BUIControl();
$menuName = $menuControl->getName();
$children = $menuControl->getChildren();
$childrenAsLeaf = $menuControl->getChildrenAsLeaf();
$wxAppId = $space->getAttribute('wechat_appid')->getValue();
$urlPrefix = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=';
$urlPrefix .= $wxAppId;
$urlPrefix .= '&redirect_uri=http://hamshun.sinaapp.com/97_2.php?T=';
$urlSurfix = '&response_type=code&scope=snsapi_base&state=1#wechat_redirect';
?>
<script type="text/javascript">
    $(function() {
        $('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title', 'Collapse this branch');
        $('.tree li.parent_li > span').on('click', function(e) {
            var children = $(this).parent('li.parent_li').find(' > ul > li');
            if (children.is(":visible")) {
                children.hide('fast');
                $(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');
            } else {
                children.show('fast');
                $(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');
            }
            e.stopPropagation();
        });

    });
    function SelectMenuType() {
        var MenuTypeList = document.getElementById("MenuTypeList");
        var content = MenuTypeList.options[MenuTypeList.selectedIndex].value;
        if (content == "0")
        {
            document.getElementById("textarea").style.display = '';
            document.getElementById("otherLink").style.display = 'none';
        }
        if (content == "1")
        {
            document.getElementById("textarea").style.display = 'none';
            document.getElementById("otherLink").style.display = '';
        }
    }
    function del(pId,id) {
        if (confirm("确认删除此菜单项？")) {
                  $.ajax({
                    url: '<?php echo $control->getAjaxLinkUrl('deleteMenu'); ?>',
                    type: 'POST',
                    data: {id: id, parentId: pId},
                    datatype: 'json',
                    // async: false,
                    beforeSend: function() {
                        $("#ajax-loaderdiv").show();
                    },
                    success: function(data) {
                        //alert("保存成功！");
                        //alert('server data:' + data);
                        // on success do some validation or refresh the content div to display the uploaded images 
                        jQuery("#<?php echo $control->getAjaxContainerId(); ?>").html(data);
                    },
                    complete: function() {
                        $("#ajax-loaderdiv").hide();
                    },
                    error: function(data) {
                        alert("There may a error on uploading. Try again later");
                    },
                    cache: false,
                    //contentType: false,
                    //processData: false
                });
        }
    }

    function AddMenu(pId, id) {

        $("#menuform").css("display", "block");
        $("#parentid").val(pId);
        $("#id").val(id);
        $("#menu_name").val("");
        $("#menu_url").val("");

    }
    function save() {
        var menu_name = $("#menuList option:selected").val();
        //var menu_url = $("#menu_url").val();
        var display_name = $("#menu_name").val();
        var parentid = $("#parentid").val();
        var id = $("#id").val();
        $("#savebtn").css("disabled", true);

        if (id != '0')
        {
            if (menu_name != '' || menu_name != 'empty') {
                //if(menu_name = 'empty'){
                   // menu_name = $("#menu_name").val();
                //}
                //alert("菜单名称：" + menu_name + "\n" + "菜单url：" + menu_url + "\n" + "parentid:" + parentid + "\n" + "id:" + id);
                //var menuData = [];
                //menuData['name'] = menu_name;
                //menuData['parentId'] = parentid;
                $.ajax({
                    url: '<?php echo $control->getAjaxLinkUrl('saveMenu'); ?>',
                    type: 'POST',
                    data: {name: menu_name, parentId: id, displayName: display_name},
                    datatype: 'json',
                    // async: false,
                    beforeSend: function() {
                        $("#ajax-loaderdiv").show();
                    },
                    success: function(data) {
                        //alert("保存成功！");
                        //alert('server data:' + data);
                        // on success do some validation or refresh the content div to display the uploaded images 
                        jQuery("#<?php echo $control->getAjaxContainerId(); ?>").html(data);
                    },
                    complete: function() {
                        $("#ajax-loaderdiv").hide();
                    },
                    error: function(data) {
                        alert("There may a error on uploading. Try again later");
                    },
                    cache: false,
                    //contentType: false,
                    //processData: false
                });

            } else {
                alert("请输入菜单名称!");
                $("#savebtn").css("disabled", false);
            }
        } else {
            alert("请先确定添加菜单的位置");

        }

    }


    function EditMenu(pId,id) {
        $("#parentid").val(pId);
        $("#id").val(id);
        var menuNames = [];
        var menuUrls = [];
        var menuUrls2 = [];
        //menuNames[1] = '商社导航';

<?php foreach ($childrenAsLeaf as $i => $child) { ?>
            menuNames[<?php echo $i; ?>] = '<?php echo $child->getValue(); ?>';
            menuUrls[<?php echo $i; ?>] = '<?php echo $child->getMenuUrl(); ?>';
            menuUrls2[<?php echo $i; ?>] = '<?php echo $urlPrefix . $child->getMenuUrl() . $urlSurfix; ?>';
<?php } ?>

        if (id > 0) {
            $("#menu_name").val(menuNames[id]);
            $("#menu_url").val(menuUrls2[id]);
            $("#test_link").html('<a href="' + menuUrls[id] + '" target="_blank">测试链接</a>');
        }
    }

    function changeMenuList(obj) {
        var displayName;
        displayName = $("#menuList option:selected").text();
        $("#menu_name").val(displayName);
    }
</script>
<style  type="text/css">
    .tree {
        min-height:430px;
        /*    padding:19px;*/
        margin-bottom:20px;
        /* background-color:#fbfbfb; */
        /*        border:1px solid #999;*/
        -webkit-border-radius:4px;
        -moz-border-radius:4px;
        border-radius:4px;
        -webkit-box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.05);
        -moz-box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.05);
        box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.05)
    }
    .tree li {
        width:160px;
        list-style-type:none;
        margin:0;
        padding:10px 8px 0 1px;
        position:relative
    }
    .tree li::before, .tree li::after {
        content:'';
        left:-20px;
        position:absolute;
        right:auto
    }
    .tree li::before {
        border-left:1px solid #999;
        bottom:50px;
        height:100%;
        top:0;
        width:1px
    }
    .tree li::after {
        border-top:1px solid #999;
        height:20px;
        top:25px;
        width:25px
    }
    .tree li span {
        -moz-border-radius:5px;
        -webkit-border-radius:5px;
        border:1px solid #999;
        border-radius:5px;
        display:inline-block;
        padding:3px 8px;
        text-decoration:none
    }
    .tree li.parent_li>span {
        cursor:pointer
    }
    .tree>ul>li::before, .tree>ul>li::after {
        border:0
    }
    .tree li:last-child::before {
        height:30px
    }
    .tree li.parent_li>span:hover, .tree li.parent_li>span:hover+ul li span {
        background:#eee;
        border:1px solid #94a0b4;
        color:#000
    }
    .OAuth {
        padding-top: 10px;padding-bottom: 15px;float: left;font-family: '微软雅黑', '黑体', '宋体';font-size: 18px;color: #666;
    }
    .oauthSpan{
        font-size: 12px;color: #999;padding-left: 10px;
    }
    .treeMenuEdit{
        width: 18px;float: right;height: 18px;display: block;margin-left: 3px;
        background: url(<?php echo Yii::app()->request->baseUrl; ?>/images/bottom_menu.png) no-repeat;
        background-position: -58px -11px;
    }
    .treeMenuDel{
        width: 18px;float: right;height: 18px;display: block;margin-left: 3px;
        background: url(<?php echo Yii::app()->request->baseUrl; ?>/images/bottom_menu.png) no-repeat;
        background-position: -35px -11px;
    }
    .treeMenuAdd{
        width: 18px;float: right;height: 18px;display: block;margin-left: 3px;
        background: url(<?php echo Yii::app()->request->baseUrl; ?>/images/bottom_menu.png) no-repeat;
        background-position: -13px -11px;
    }
    .menuList{
        width:175px;
        margin:2px 0px 2px 100px;
    }
</style>
<div >

    <div class="OAuth">自定义菜单授权
        <span class="oauthSpan">使用本模块，必须要在微信公众平台"开发模式"下使用自定义菜单，首先要在公众平台申请自定义菜单使用的AppId和AppSecret。</span>
    </div>
    <!--        <div>
                  <ul style="margin-left: 32px;">
                      <li style="font-weight: bold;list-style-type:none;">微信应用AppId：<input type="text"  value="wxd33a8d045970d6e58" id="appid" name="appid"></li>
                  </ul>
                  <ul style="margin-left: 0px;">
                        <li style="font-weight: bold;list-style-type:none;">微信应用AppSecret：<input type="text"   value="6e18aba9074d1e8c71995c5438ebfa4dc" id="secret" name="secret"></li>
                   </ul>
            </div>-->
</div>
<div class="tree ">
    <ul>
        <li style="width: 30%;float:left;">
            <strong><i class="icon-folder-open"></i> 菜单列表</strong> <a title="添加一级菜单" onclick="AddMenu(1, 2);" href="javascript:void(0);">
                <img width="20" height="20" alt="添加一级菜单" src="<?php echo Yii::app()->request->baseUrl; ?>/images/bottom_menu.jpg">
            </a>
            <ul><?php foreach ($children as $id => $child) { ?>
                    <li>
                        <span><i class="icon-minus-sign"></i> <?php echo $child->getValue(); ?></span>
                        <a class="treeMenuEdit"  href="javascript:void(0);" onclick="EditMenu(<?php echo $child->getParent()->id; ?>,<?php echo $id; ?>);" title="编辑"></a>
                        <a  class="treeMenuDel" href="javascript:void(0);" onclick="del(<?php echo $child->getParent()->id; ?>,<?php echo $id; ?>);" title="删除"></a>
                        <a  class="treeMenuAdd"  href="javascript:void(0);" onclick="AddMenu(<?php echo $child->getParent()->id; ?>,<?php echo $id; ?>);" title="添加"></a>
                        <ul>
                            <?php
                            $children2 = $child->getChildren();
                            foreach ($children2 as $id => $child) {
                                ?>
                                <li>
                                    <span><i class="icon-leaf"></i> <?php echo $child->getValue(); ?></span>
                                    <a  class="treeMenuEdit" href="javascript:void(0);" onclick="EditMenu(<?php echo $child->getParent()->id; ?>,<?php echo $id; ?>);" title="编辑"></a>
                                    <a  class="treeMenuDel" href="javascript:void(0);" onclick="del(<?php echo $child->getParent()->id; ?>,<?php echo $id; ?>);" title="删除"></a>
                                </li>
                            <?php } ?>
                        </ul>
                    </li>
                <?php } ?>
            </ul>
        </li>
        <li style="width: 60%;font-size: 10px;float:right">
            <div>

                <form class="form-horizontal">
                    <fieldset>
                        <div class="control-group" id="menuform">
                            <label class="control-label" for="input01" style="width: 100px;">菜单标题：</label>
                            <div class="controls" style="margin-left:10px;">                                
                                <input type="text" class="input-xlarge" id="menu_name" name="menu_name" style="width: 160px;" value="菜单标题">
                            </div>
                            <div>                                
                                <?php
                                echo CHtml::dropDownList('menuList', key($registeredList), $registeredList, array(
                                    'onChange' => 'changeMenuList(this)','class'=>'menuList'));
                                ?>
                            </div>

                            <div class="help-block" style="margin-left: 30px;
                               width: 260px;">
                                注：一级菜单文字不超过4个汉字或8个字母，
                                二级菜单标题最多不得超过8个汉字或16个字母。
                                一级菜单个数范围：2-3个，二级菜单个数范围：2-5个，菜单最多支持两层。 
                            </div>


                            <label class="control-label" style="margin-left:-68px" for="textarea">菜单URL：</label>
                            <div class="controls" style="margin-left: 30px;">
                                <textarea class="input-xlarge" id="menu_url" name="menu_url" rows="3"></textarea>
                            </div>
                            <br>
                            <label class="control-label" style="margin-left:-68px" for="textarea">测试URL：</label>
                            <div class="controls" style="margin-left: 50px;">
                                <div id='test_link' class='btn'>测试URL</div>
                            </div>

                        </div>                    

                        <div class="form-actions" style="margin-left: 30px;width: 210px;padding-left: 90px;">
                            <input type="hidden" id="parentid" value="0" />
                            <input type="hidden" id="id" value="0" />
                            <input type="button" class="btn btn-primary" onclick="save();" value="保 存"  id="savebtn">
                            <?php echo $control->getAjaxLink(array('name' => '发布到微信', 'id' => 'releaseMenu'), array('operationId' => $operation->id,'htmlOptions' => array('class' => 'btn btn-primary'))); ?>

                        </div>
                    </fieldset>
                </form>
            </div>
        </li>
    </ul>
</div>
